package com.jza04.flightbooking.service.impl;

import com.jza04.flightbooking.dto.FlightRevenueDTO;
import com.jza04.flightbooking.entity.Flight;
import com.jza04.flightbooking.entity.Passenger;
import com.jza04.flightbooking.entity.Ticket;
import com.jza04.flightbooking.mapper.TicketMapper;
import com.jza04.flightbooking.service.FlightTicketService;
import com.jza04.flightbooking.service.PassengerService;
import com.jza04.flightbooking.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 购票服务实现类
 */
@Service
public class TicketServiceImpl implements TicketService {

    @Autowired
    private TicketMapper ticketMapper;
    
    @Autowired
    private FlightTicketService flightTicketService;
    
    @Autowired
    private PassengerService passengerService;
    
    @Override
    public List<Ticket> findAllTickets() {
        return ticketMapper.findAll();
    }
    
    @Override
    public Ticket findTicketById(Integer ticketId) {
        return ticketMapper.findById(ticketId);
    }
    
    @Override
    public List<Ticket> findTicketsByFlightId(String flightId) {
        return ticketMapper.findByFlightId(flightId);
    }
    
    @Override
    public List<Ticket> findTicketsByPassengerId(Integer passengerId) {
        return ticketMapper.findByPassengerId(passengerId);
    }
    
    @Override
    @Transactional
    public boolean bookTicket(Ticket ticket) {
        // 设置购票时间
        if (ticket.getPurchaseDate() == null) {
            ticket.setPurchaseDate(new Date());
        }
        
        // 设置默认状态
        if (ticket.getStatus() == null || ticket.getStatus().isEmpty()) {
            ticket.setStatus("已预订");
        }
        
        // 减少航班剩余座位数
        if (!flightTicketService.decreaseRemainingSeats(ticket.getFlightId())) {
            return false;
        }
        
        // 插入购票记录
        boolean result = ticketMapper.insert(ticket) > 0;
        
        // 如果购票成功，则更新旅客的购票总金额
        if (result) {
            passengerService.updateTotalPurchase(ticket.getPassengerId(), ticket.getActualPrice());
        }
        
        return result;
    }
    
    @Override
    @Transactional
    public boolean updateTicket(Ticket ticket) {
        return ticketMapper.update(ticket) > 0;
    }
    
    @Override
    @Transactional
    public boolean cancelTicket(Integer ticketId) {
        Ticket ticket = findTicketById(ticketId);
        if (ticket == null) {
            return false;
        }
        
        // 更新票状态为已取消
        boolean result = updateTicketStatus(ticketId, "已取消");
        
        // 如果取消成功，则恢复航班座位
        if (result) {
            flightTicketService.increaseRemainingSeats(ticket.getFlightId());
        }
        
        return result;
    }
    
    @Override
    @Transactional
    public boolean updateTicketStatus(Integer ticketId, String status) {
        return ticketMapper.updateStatus(ticketId, status) > 0;
    }
    
    @Override
    public BigDecimal getTotalRevenue() {
        BigDecimal totalRevenue = ticketMapper.calculateTotalRevenue();
        return totalRevenue != null ? totalRevenue : BigDecimal.ZERO;
    }
    
    @Override
    public BigDecimal getRevenueByDateRange(Date startDate, Date endDate) {
        BigDecimal revenue = ticketMapper.calculateRevenueByDateRange(startDate, endDate);
        return revenue != null ? revenue : BigDecimal.ZERO;
    }
    
    @Override
    public Map<String, BigDecimal> getRevenueByFlight() {
        List<FlightRevenueDTO> revenueList = ticketMapper.calculateRevenueByFlight();
        Map<String, BigDecimal> revenueMap = new HashMap<>();
        
        for (FlightRevenueDTO item : revenueList) {
            revenueMap.put(item.getFlightId(), item.getRevenue());
        }
        
        return revenueMap;
    }
}